package com.qf.controller;

import com.qf.pojo.Goods;
import com.qf.service.GoodsService;
import com.qf.vo.Msg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @RequestMapping("all")
    @ResponseBody
    public Msg<Goods> selectAll(){
        List<Goods> list = goodsService.selectAll();
        Msg<Goods> msg=new Msg<>();
        msg.setData(list);
        msg.setCode(0);
        msg.setCount(list.size());
        return msg;
    }
    @RequestMapping("One/{mess}")
    @ResponseBody
    public Msg<Goods> selectOne(@PathVariable("mess") String mess){
        List<Goods> list = goodsService.selectOne(mess);
        Msg<Goods> msg=new Msg<>();
        msg.setData(list);
        return msg;
    }
    @RequestMapping("type/{mess}")
    public Msg selectByType(@PathVariable("mess") String mess){
        List<Goods> list = goodsService.selectByType(mess);
        Msg<Goods> msg=new Msg<>();
        msg.setData(list);
        return msg;
    }
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody
    public Msg<Goods> update( Goods goods) throws UnsupportedEncodingException {
        Msg<Goods> msg=new Msg<>();
        //goods.setGname(new String(goods.getGname().getBytes("iso-8859-1"),"utf-8"));
        System.out.println("goods = " + goods);
        int i = goodsService.update(goods);
        if(i==1){
            msg.setMsg("修改成功");
            msg.setCode(1);
            return msg;
        }
        return null;
    }
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ResponseBody
    public Msg<Goods> add(Goods goods) throws UnsupportedEncodingException {
        Msg<Goods> msg=new Msg<>();
        goods.setGname(new String(goods.getGname().getBytes("iso-8859-1"),"utf-8"));
        int i = goodsService.add(goods);
        if(i==1){
            msg.setMsg("添加成功");
            msg.setCode(1);
            return msg;
        }
        return null;
    }
    @RequestMapping("del/{id}")
    @ResponseBody
    public Msg<Goods> delete(@PathVariable("id") int id){
        Msg<Goods> msg=new Msg<>();
        int i = goodsService.delete(id);
        if(i==1){
            msg.setMsg("删除成功");
            msg.setCode(1);
            return msg;
        }
        return null;
    }
    @RequestMapping("dels")
    @ResponseBody
    public Msg deleteAll(String id){
        String []ids=id.split(",");
        List<Integer> list=new ArrayList<>();
        for(int i=0;i<ids.length;i++){
            list.add(Integer.valueOf(ids[i]));
        }
        int i=goodsService.deleteAll(list);
        Msg<Goods> msg=new Msg<>();
        if(i==1){
            msg.setCode(1);
            msg.setMsg("删除成功");
            return msg;
        }
        return null;
    }
}
